<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>

import { we_auth, get_info, wx_share, add_inviter, get_goods_detail, get_pay_detail, get_column_show, get_activity_show, getAudioDetail, getFreeAudioShow1, getThreeShow } from '@/api/api'
import { AlertModule } from 'vux'
import axios from 'axios'
import {wxshare} from '@/config/wxshare'
export default {
  name: 'app',
  data () {
    return {
      pid: 0,
      url: window.location.href,
    }
  },
  created () {    
    // sessionStorage.setItem('uid', 146)
    this.judge()
    var _url = window.location.href;
    if(_url.indexOf('&state=&') > 0){
      var code_index = _url.indexOf('&code')
      var code_none_index = _url.indexOf('&state=#')
      var code = _url.substring(code_index+6, code_none_index)
      sessionStorage.setItem('code', code)
    }else{
      var code_index = _url.indexOf('code')
      var code_none_index = _url.indexOf('&')
      var code = _url.substring(code_index+5, code_none_index)
      sessionStorage.setItem('code', code)
    }
    if(sessionStorage.code && !sessionStorage.uid && sessionStorage.code.length > 10){
      this.getInfo()
    }
  },
  mounted () {
  },
  watch: {
    "$route"(to, from) {
      this.fenxiang()
    }
  },
  methods: {
    // 判断是否存在
    judge () {
      var pid = 0;
      if(window.location.href.indexOf('pid')>0){   
        var _index = this.url.indexOf('#')
        var _index2 = this.url.indexOf('pid')
        this.url = 'http://m.aihera.cn/web-xiala/dist/' + window.location.href.substring(_index, _index2-1)
        // this.judge()
        pid = this.$route.query.pid
      }
      if(!sessionStorage.code){
        var jsonData = {
          url: this.url,
          pid: pid
        }
        we_auth(jsonData).then((res) => {
          console.log(res)
          location.href=res.data.info
          sessionStorage.setItem('code', 'undifined')
        }).catch((err) => {
          console.log(err)
          sessionStorage.removeItem('code')
        })
      }     
    },
    getInfo () {
      get_info(sessionStorage.code).then((res) => {
        sessionStorage.setItem('uid', res.data.info.id)
        if(sessionStorage.uid){
          this.fenxiang()
          this.wx_shares()
          axios.get('http://m.aihera.cn/index.php/home/user/get_user_info/uid/'+sessionStorage.uid).then((res) => {
            if(res.data.status == 1 && res.data.info.username){    
              sessionStorage.setItem('phone', res.data.info.username)
            }else{
              sessionStorage.removeItem('phone')
            }
          }).catch((response) => {
            console.dir(response)
          })  
        }
        if(sessionStorage.uid){
          // this.pid = sessionStorage.pid
          this.add_inviters()
        }
        sessionStorage.setItem('info', JSON.stringify(res.data.info))
      }).catch((err) => {
        console.dir(err)
      })
    },
    wx_shares () {
      var _url = window.location.href;
      if(window.location.href.indexOf('?pid=')>0){   
        var _index1 = window.location.href.indexOf('?pid=');
        _url = window.location.href.substring(0, _index1);
      }else if(window.location.href.indexOf('&pid=')>0){
        var _index2 = window.location.href.indexOf('&pid=');
        _url = window.location.href.substring(0, _index2);
      }
      var jsonData = {
        url: _url,
        uid: sessionStorage.uid
      }
      wx_share(jsonData).then((res) => {
        if(sessionStorage.shareCons){
          var shareCons = JSON.parse(sessionStorage.shareCons);
          res.data.info.desc = shareCons.desc
          res.data.info.imgurl = shareCons.imgurl
          res.data.info.title = '孕伴 - ' + shareCons.title
        }
        wxshare(res, (res) => {
        }, (err) => {
          console.log(err)
        })
      }).catch((err) => {
        console.log(err)
      })
    },
    add_inviters () {
      if(window.location.href.indexOf('pid')>0){
        var pid = this.$route.query.pid;
        if(pid > 0){
          add_inviter(sessionStorage.pid).then((res) => {
          }).catch((err) => {
            console.dir(err)
          })
        }
      }      
    },
    //  特定分享
    fenxiang () {

      let currentRouter = this.$router.currentRoute.fullPath; 
      if(sessionStorage.shareCons){
        sessionStorage.removeItem('shareCons')
      }   
      if(sessionStorage.uid){
        if(currentRouter.indexOf('goodsDetails') > 0){    
          let goodsId = this.$route.query.goodsId;
          get_goods_detail(goodsId).then((res) => {
            var shareCons = {
              imgurl: res.data.info.img,
              desc: '优惠价： ￥'+ res.data.info.price,
              title: res.data.info.title
            }
            sessionStorage.setItem('shareCons', JSON.stringify(shareCons))
            this.wx_shares()
          }).catch((err) => {
            console.log(err)
          })
        }else if(currentRouter.indexOf('payDetials') > 0){
          var id = this.$route.query.id
          var jsonData = {
            id: id
          }
          get_pay_detail(jsonData).then((res) => {
            var shareCons = {
              imgurl: res.data.info.img,
              desc: res.data.info.intro,
              title: res.data.info.title
            }
            sessionStorage.setItem('shareCons', JSON.stringify(shareCons))
            this.wx_shares()
          }).catch((err) => {
            console.log(err)
          })
        }else if(currentRouter.indexOf('audioDetails') > 0){
          var id = this.$route.query.id
          var is_three = this.$route.query.is_three+''
          if(is_three == 'true'){            
            getFreeAudioShow1(id).then((res) => {
              var shareCons = {
                imgurl: res.data.info.img,
                desc: res.data.info.share_desc,
                title: res.data.info.title
              }
              sessionStorage.setItem('shareCons', JSON.stringify(shareCons))
              this.wx_shares()
            }).catch((err) => {
              console.dir(err)
            })
          }else{            
            getThreeShow(id).then((res) => {
              var shareCons = {
                imgurl: res.data.info.img,
                desc: res.data.info.share_desc,
                title: res.data.info.title
              }
              sessionStorage.setItem('shareCons', JSON.stringify(shareCons))
              this.wx_shares()
            }).catch((err) => {
              console.log(err)
            })
          }
        }else if(currentRouter.indexOf('activityDetails') > 0){
          var activity_id = this.$route.query.activity_id
          get_activity_show(activity_id).then((res) => {
            var shareCons = {
              imgurl: res.data.info.img,
              desc: res.data.info.share_desc,
              title: res.data.info.title
            }
            sessionStorage.setItem('shareCons', JSON.stringify(shareCons))
            this.wx_shares()
          }).catch((err) => {
            console.dir(err)
          })
        }else if(currentRouter.indexOf('payDetials') > 0){
          var column_id = this.$route.query.column_id
          get_column_show(column_id).then((res) => {
            var shareCons = {
              imgurl: res.data.info.image,
              desc: res.data.info.introduce,
              title: res.data.info.title
            }
            sessionStorage.setItem('shareCons', JSON.stringify(shareCons))
            this.wx_shares()
          }).catch((err) => {
            console.dir(err)
          })
        }else{
          this.wx_shares()
        }
      } 
    } 
  }
}
</script>

<style>
</style>
